package com.practice.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.common.constant.MessageConstant;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.admin.AdvimgMapper;
import com.practice.mapper.admin.AdvposMapper;
import com.practice.pojo.admin.dto.AdvimgDTO;
import com.practice.pojo.admin.dto.AdvimgPageDTO;
import com.practice.pojo.admin.entity.Advimg;
import com.practice.pojo.admin.entity.Advpos;
import com.practice.pojo.admin.vo.AdvimgVO;
import com.practice.service.admin.AdvimgService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class AdvimgServiceImpl extends ServiceImpl<AdvimgMapper, Advimg> implements AdvimgService {


    @Autowired
    private AdvimgMapper advimgMapper;

    @Autowired
    private AdvposMapper advposMapper;

    //广告图详情
    @Override
    public Result advimgDetail(Integer id) {
        Advimg advimg = advimgMapper.selectById(id);
        return Result.success(advimg);
    }


    //广告图删除
    @Override
    public Result advimgDelete(Integer id) {
        //非空判断
        if (id == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        advimgMapper.deleteById(id);
        return Result.success();
    }


    //广告图列表-分页查询
    @Override
    public Result pageList(AdvimgPageDTO advimgPageDTO) {
        //非空判断
        if (advimgPageDTO.getPage() == null) {
            //设置当前页默认值
            advimgPageDTO.setPage(1);
        }
        if (advimgPageDTO.getLimit() == null) {
            //设置总页默认值
            advimgPageDTO.setLimit(10);
        }

        //创建IPage分页对象,设置分页参数
        IPage<Advimg> page = new Page<>(advimgPageDTO.getPage(), advimgPageDTO.getLimit());

        //执行分页条件查询
        LambdaQueryWrapper<Advimg> lqw = new LambdaQueryWrapper<Advimg>();
        if (advimgPageDTO.getAdvimgdesc() != null) {
            lqw.like(Advimg::getAdvimgdesc, advimgPageDTO.getAdvimgdesc());
        }
        if (advimgPageDTO.getAdvimgpos() != null) {
            lqw.like(Advimg::getAdvimgpos, advimgPageDTO.getAdvimgpos());
        }

        IPage<Advimg> page2 = advimgMapper.selectPage(page, lqw);

        //拷贝集合page2.getRecords()并生成新的AdvimgVO集合
        List<AdvimgVO> voList = BeanUtil.copyToList(page2.getRecords(), AdvimgVO.class);

        //遍历voList集合，根据广告位id查询广告位名字advposname并赋值
        for (AdvimgVO advimgVO : voList) {
            String id = advimgVO.getAdvimgpos();
            Advpos advpos = advposMapper.selectById(id);
            advimgVO.setAdvposname(advpos.getAdvposname());
        }

        //page2对象属性值封装到PageResult对象内并返回
        PageResult pageResult = new PageResult();
        pageResult.setCount(page2.getTotal());
        pageResult.setTotalPages(page2.getPages());
        pageResult.setPageSize(page2.getSize());
        pageResult.setCurrentPage(page2.getCurrent());
        pageResult.setData(voList);

        return Result.success(pageResult);
    }



    //广告图修改
    @Override
    public Result advimgUpdate(AdvimgDTO advimgDTO) {
        //非空判断
        if(advimgDTO.getId()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        Advimg advimg = new Advimg();
        BeanUtils.copyProperties(advimgDTO,advimg);
        advimgMapper.updateById(advimg);

        return Result.success();
    }


    //广告图添加
    @Override
    public Result advimgAdd(AdvimgDTO advimgDTO) {
        Advimg advimg = new Advimg();
        BeanUtils.copyProperties(advimgDTO,advimg);
        advimgMapper.insert(advimg);
        return Result.success();
    }
}
